package cn.keniykey.mc.landterritory;

import java.util.logging.Logger;

import org.bukkit.Bukkit;
import org.bukkit.event.HandlerList;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import cn.keniykey.mc.landterritory.command.landadmin.CommandLandAdminMain;
import cn.keniykey.mc.landterritory.command.only.CommandGo;
import cn.keniykey.mc.landterritory.command.only.CommandLand;
import cn.keniykey.mc.landterritory.command.only.CommandLands;
import cn.keniykey.mc.landterritory.config.ConfigurationManager;
import cn.keniykey.mc.landterritory.config.LanguageManager;
import cn.keniykey.mc.landterritory.config.ViewsConfiguration;
import cn.keniykey.mc.landterritory.config.WorldsConfiguration;
import cn.keniykey.mc.landterritory.database.DataBaseManager;
import cn.keniykey.mc.landterritory.event.LandTerritoryReloadConfigEvent;
import cn.keniykey.mc.landterritory.land.LandEnterLeaveCheck;
import cn.keniykey.mc.landterritory.land.LandManager;
import cn.keniykey.mc.landterritory.land.LandRangeToolListener;
import cn.keniykey.mc.landterritory.land.PlayerJoinQuitListener;
import cn.keniykey.mc.landterritory.land.RangeShowManager;
import cn.keniykey.mc.landterritory.land.api.ELandType;
import cn.keniykey.mc.landterritory.landflag.LandFlagManager;
import cn.keniykey.mc.landterritory.landflag.api.LandFlagConfigurationFile;
import cn.keniykey.mc.landterritory.landview.LandChooseView;
import cn.keniykey.mc.landterritory.landview.LandCreateView;
import cn.keniykey.mc.landterritory.landview.LandFlagAddView;
import cn.keniykey.mc.landterritory.landview.LandFlagInfoView;
import cn.keniykey.mc.landterritory.landview.LandFlagListView;
import cn.keniykey.mc.landterritory.landview.LandFlagRestView;
import cn.keniykey.mc.landterritory.landview.LandInfoView;
import cn.keniykey.mc.landterritory.landview.LandInputView;
import cn.keniykey.mc.landterritory.landview.LandListView;
import cn.keniykey.mc.landterritory.landview.LandMainView;
import cn.keniykey.mc.landterritory.landview.LandQueryView;
import cn.keniykey.mc.landterritory.landview.LandRangeCreateView;
import cn.keniykey.mc.landterritory.landview.LandSoldInfoView;
import cn.keniykey.mc.landterritory.landview.LandSoldListView;
import cn.keniykey.mc.landterritory.landview.LandViewContentShare;
import cn.keniykey.mc.landterritory.toolkit.PackageUtil;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameView;
import cn.keniykey.mc.viewlibrary.view.api.AbFrameViewContentShare;

public final class LandTerritoryMain extends JavaPlugin{
	
	private static LandTerritoryMain plugin;
	
	public static LandTerritoryMain getPlugin() {
		return plugin;
	}
	@Override
	public void onLoad() {
		plugin = this;
	}
	
	@Override
	public void onEnable() {
		final Logger logger = this.getLogger();
		logger.info("插件正在启用中，版本为：" + this.getDescription().getVersion());
		try {
			this.loadViews(logger);
			if(!this.loadDataBase(logger)) {
				this.getPluginLoader().disablePlugin(this);
				return;
			}
			this.loadLandFlags(logger);
			this.loadConfig(logger);
			this.loadListeners(logger);
			this.loadCommands(logger);
		}catch(Exception ex) {
			logger.warning("插件启用失败");
			ex.printStackTrace();
			this.getPluginLoader().disablePlugin(this);
			return;
		}
		logger.info("插件启用完成");
	}
	@Override
	public void onDisable() {
		final Logger logger = this.getLogger();
		logger.info("插件正在关闭中，版本为：" + this.getDescription().getVersion());
		AbFrameViewContentShare.cancellationAbFrameViewContentShare(this);
		AbFrameView.unRegisterAbframeView(this);
		logger.info("与ViewLibrary取消功能注册");
		this.getServer().getScheduler().cancelTasks(this);
		HandlerList.unregisterAll(this);
		DataBaseManager.closeDataBaseCore();
		logger.info("数据库已关闭");
		logger.info("插件关闭完成");
	}
	private void loadCommands(final Logger logger) {
		logger.info("正在注册命令......");
		this.getCommand("land").setExecutor(new CommandLand());
		this.getCommand("lands").setExecutor(new CommandLands());
		this.getCommand("go").setExecutor(new CommandGo());
		this.getCommand("landadmin").setExecutor(new CommandLandAdminMain());
		logger.info("注册命令完成");
	}
	private void loadListeners(final Logger logger) {
		logger.info("正在注册监听与循环任务......");
		final PluginManager m = this.getServer().getPluginManager();
		m.registerEvents(new LandRangeToolListener(), this);
		m.registerEvents(new PlayerJoinQuitListener(), this);
		m.registerEvents(new RangeShowManager(), this);
		Bukkit.getScheduler().runTaskTimer(this, new LandEnterLeaveCheck(), 100l, Math.max(ConfigurationManager.LandInOut_Tick, 1l));
		logger.info("注册监听与循环任务完成");
	}
	private boolean loadDataBase(final Logger logger) {
		logger.info("正在连接数据库，加载数据中......");
		try {
			DataBaseManager.enableDataBaseCore();
			logger.info("已连接到数据库，正在使用 " + DataBaseManager.getDataBaseCore().getDataBase().toString() + " 数据库");
		}catch(Exception ex) {
			ex.printStackTrace();
			logger.warning("无法连接数据库");
			return false;
		}
		try {
			LandManager.loadLands();
		}catch(Exception ex) {
			ex.printStackTrace();
			logger.warning("无法加载数据");
			DataBaseManager.closeDataBaseCore();
			return false;
		}
		long amount = 0;
		for(ELandType type : ELandType.values()) amount += LandManager.getLandManager().getServerLands(type).size();
		logger.info("加载数据完成，已加载领地 " + amount + " 个");
		return true;
	}
	private void loadLandFlags(final Logger logger) {
		logger.info("正在加载领地标记中......");
		PackageUtil.loader = this.getClassLoader();
		LandFlagManager.getFlagManager().loadFlags();
		logger.info("加载领地标记完成，已加载" + LandFlagManager.getFlagManager().getLandFlags().size() + "个");
	}
	private void loadViews(final Logger logger) {
		logger.info("正在与ViewLibrary注册View中......");
		logger.info("正在注册ViewActivity中......");
		AbFrameViewContentShare.registerAbFrameViewContentShare(this, LandViewContentShare.class);
		logger.info("正在注册通用View中......");
		AbFrameView.registerAbFrameView(this, LandChooseView.class);
		AbFrameView.registerAbFrameView(this, LandInputView.class);
		logger.info("正在注册主要View中......");
		AbFrameView.registerAbFrameView(this, LandQueryView.class);
		AbFrameView.registerAbFrameView(this, LandMainView.class);
		AbFrameView.registerAbFrameView(this, LandCreateView.class);
		AbFrameView.registerAbFrameView(this, LandRangeCreateView.class);
		AbFrameView.registerAbFrameView(this, LandListView.class);
		AbFrameView.registerAbFrameView(this, LandSoldListView.class);
		AbFrameView.registerAbFrameView(this, LandInfoView.class);
		AbFrameView.registerAbFrameView(this, LandSoldInfoView.class);
		AbFrameView.registerAbFrameView(this, LandFlagListView.class);
		AbFrameView.registerAbFrameView(this, LandFlagAddView.class);
		AbFrameView.registerAbFrameView(this, LandFlagInfoView.class);
		AbFrameView.registerAbFrameView(this, LandFlagRestView.class);
		logger.info("与ViewLibrary注册View完成");
	}
	private void loadConfig(final Logger logger) {
		logger.info("正在加载配置文件中......");
		ConfigurationManager.reloadConfiguration();
		LanguageManager.reloadLanguageConfiguration();
		ViewsConfiguration.reloadViewsConfiguration();
		WorldsConfiguration.reloadWorldsConfiguration();
		LandFlagConfigurationFile.load();
		Bukkit.getPluginManager().callEvent(new LandTerritoryReloadConfigEvent());
		LandFlagConfigurationFile.changeSave();
		logger.info("加载配置文件完成");
	}
}